import unittest
from localfs import LocalFileSystem
from simpletagfs import SimpleTagFS
#from tagfs import TagFSPath
import fs


class TestFS(unittest.TestCase):
    def setUp(self):
        pass


    def tearDown(self):
        pass
    
    
    def testLocalFS(self):
        try:
            lfs = LocalFileSystem("xl:\\")
            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, OSError))
            
        try:
            lfs = LocalFileSystem("C:\\boot.ini")
            self.fail("no fail")
        except Exception as e:
            self.assertTrue(isinstance(e, OSError))
            
        try:
            lfs = LocalFileSystem(".")
        except:
            self.fail("no fail")
        
        path = "Shit/jjj/lald/"
        abspath = lfs.abspath(path)
        self.assertEqual(abspath, "/Shit/jjj/lald")
        
        path = "/Shit/../lald/"
        abspath = lfs.abspath(path)
        self.assertEqual(abspath, "/lald")
        
        path = "./Shit/jjj/lald/"
        abspath = lfs.abspath(path)
        self.assertEqual(abspath, "/Shit/jjj/lald")
        
        path = "."
        abspath = lfs.abspath(path)
        self.assertEqual(abspath, "/")
        
        path = "/"
        abspath = lfs.abspath(path)
        self.assertEqual(abspath, "/")
        
        try:
            path = "abc/../../abcd"
            abspath = lfs.abspath(path)
            self.fail("no fail")
        except Exception as e:
            self.assertTrue(isinstance(e, ValueError))
          
        # change dir  
        lfs.setWorkingDirectory("cheat/dick")

        path = "abc/../../abcd"
        abspath = lfs.abspath(path)
        self.assertEqual(abspath, "/cheat/abcd")
        
        path = "../../"
        abspath = lfs.abspath(path)
        self.assertEqual(abspath, "/")

        try:
            f = lfs.create("shit")
            self.fail("no fail")
        except Exception as e:
            self.assertTrue(isinstance(e, OSError))
        
        
        f = lfs.create("/shit.txt")
        f.open(fs.ACCESS_RW)
        f.write("shit happens.".encode())
        f.close()
        
        lfs.delete("/shit.txt")
    
    def testSimpleTagFS(self):
        basefs = LocalFileSystem("F:\\Temp\\tagfs")
        tagfs = SimpleTagFS(basefs)
        
        f = tagfs.create("shit.txt")
        mypath = f.path()
        f.open()
        f.write("omg".encode())
        f.close()
        
        f1 = tagfs.open(mypath)
        f1.write("xxxxxxxx".encode())
        f1.close()
    
   

suite = unittest.TestLoader().loadTestsFromTestCase(TestFS)
if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite)